package com.boarsoft.boar.app.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.app.AppLibBiz;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.entity.AppLib;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.dao.PagedResult;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@RestController
@RequestMapping("/app/lib")
public class AppLibAction {
	@Autowired
	private AppLibBiz appLibBiz;

	/**
	 * 保存appId与libId的一对多关系
	 * 
	 * @return
	 */
	@RequestMapping("/save.do")
	@Authorized(code = "app.lib.save")
	public ReplyInfo<Object> save(String appIds, String libIds) {
		List<AppLib> appLibs = appLibBiz.findByBatch(appIds, libIds);
		if (appLibs.size() > 0) {
			return new ReplyInfo<Object>(Constants.ERROR_DUPLICATED, "Msg.common.param");
		}
		appLibBiz.save(appIds, libIds);
		return ReplyInfo.SUCCESS;
	}

	/**
	 * 修改appId与libId的一对多关系 目前只支持修改libPath字段
	 * 
	 * @return
	 */
	@RequestMapping("/update.do")
	@Authorized(code = "app.lib.update")
	public ReplyInfo<Object> update(String id, String libPath) {
		AppLib appLib = appLibBiz.get(id);
		appLib.setLibPath(libPath);
		appLibBiz.save(appLib);
		return ReplyInfo.SUCCESS;
	}

	/**
	 * 修改appId与libId的一对多关系
	 * 
	 * @return
	 */
	@RequestMapping("/remove.do")
	@Authorized(code = "app.lib.remove")
	public ReplyInfo<Object> remove(String id) {
		appLibBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}

	/**
	 * 根据appId分页查询AppLib
	 * 
	 * @return
	 */
	@JsonIgnoreProperties({ "svcs", "libs", "insts", "apps", "files", "protocols" })
	@RequestMapping("/list.do")
	@Authorized(code = "app.lib.list")
	public ReplyInfo<Object> list(String id, String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<AppLib> pr = appLibBiz.list(id, key, orderBy, pageNo, pageSize);
		return new ReplyInfo<Object>(true, pr);
	}
}